﻿using HHG.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace OnLineExam.Web.Extensions
{
    public class ResultData
    {
        public int allwort { get; set; }
        public int hasCount { get; set; }
        public ResultData(int allwort, int has)
        {
            this.allwort = allwort;
            this.hasCount = has;
        }
        public static ResultData Create(int allwort, int has)
        {
            return new ResultData(allwort, has);
        }
    }
    public static class PaperExtension
    {
       
        public static ResultData GetExamCount(string userId, int paperId,int allwort)
        {
           
            using (var ctx = new OnLineExam.Data.OnlineExamEntities())
            {
                var userAnswers = ctx.UserTestInfo.AsNoTracking().Where(s=>s.UserID==userId && s.PaperID==paperId && s.Type=="进入考试" );

                var hasCount = userAnswers.Count();
                if (userId.StartsWith("admin")) return ResultData.Create(allwort, hasCount);

                return ResultData.Create(allwort- hasCount, hasCount);
            }

            }

            public static ResultCode CheckIsAllowExam(string userId,int paperId) {
            if (userId.StartsWith("admin")) return ResultCode.SetPass("管理员级别通行证");

            if (!userId.HasValue()) return ResultCode.SetFail("用户ID异常");

            if (paperId<=0) return ResultCode.SetFail("试卷ID异常");

            using (var ctx=new OnLineExam.Data.OnlineExamEntities())
            {
                var paper = ctx.Paper.FirstOrDefault(s=>s.PaperID==paperId);
                if(paper==null) return ResultCode.SetFail("无此试卷");


                if (!paper.joinemps.Split(',').ToList().Any(s=>s.Contains(userId)))
                {
                    return ResultCode.SetFail("您不在考试名单中，无法继续！");
                   
                }
                if (paper.startExamTime > DateTime.Now)
                {
                    return ResultCode.SetFail("尚未开始考试！");
                    
                }
                if (paper.EndExamTime.Value < DateTime.Now)
                {
                    return ResultCode.SetFail("试卷已经过期！");
                    
                }
                //最高得分
                var scores = ctx.Score.AsNoTracking().Where(s => s.UserID == userId && s.PaperID == paperId) ;
                var maxUserScore = 0;
                if (scores.Any())
                {
                    maxUserScore = scores.Max(s => s.UserScore);

                }
                if (maxUserScore>=paper.passMark)//检查是否分数通过
                {
                    return ResultCode.SetFail("您已经通过考试，无需重复参加！");
                    
                }

                if (ctx.UserTestInfo.AsNoTracking().Where(s=>s.UserID==userId && s.PaperID==paperId && s.Type== "进入考试").Count()>=paper.allowExamNum)
                {
                    return ResultCode.SetFail("您已经进入" + paper.allowExamNum + "次考试，无法继续！"); 
                }

                if (ctx.UserAnswer.AsNoTracking().Where(s => s.UserID == userId && s.PaperID == paperId && s.Type == "开始考试").Count() >= paper.allowExamNum)
                {
                    return ResultCode.SetFail("您已经参加过" + paper.allowExamNum + "次考试，无法继续！");

                }

                return ResultCode.SetPass("校验通过，可以考试");

            }

        }
    }
}